package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.DishFlavorMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description DishSerevicelmpl
 * @Author zhengyuan&junjun
 * @Date 2024-10-02
 */
@Service
public class DishSerevicelmpl implements DishService {
   @Autowired
    private  DishMapper dishMapper;
    @Autowired
   private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    public PageResult page(DishPageQueryDTO dto) {

        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        Page<DishVO> p =  dishMapper.page(dto);




        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional
    public void update(DishDTO dto) {

        //创建实体对象
        Dish dish = new Dish();
        //将dto内的属性放进dish对象中
        BeanUtils.copyProperties(dto,dish);

        //向数据库中插入一条数据
        dishMapper.update(dish);


        //获取菜品的Id
        Long dishId = dish.getId();


        List<DishFlavor> flavors =  dto.getFlavors();

        if (flavors != null && flavors.size() >0){

            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
             //向数据库插入数据
            dishFlavorMapper.flavorsInsert(flavors);




        }



    }

    @Override
    public DishVO getById(Long dishId) {

       DishVO dishVO =  dishMapper.getById(dishId);

        return dishVO;
    }

    @Override
    public void insert(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        //修改dish表中的数据
        dishMapper.update1(dish);

        //删除口味表中的数据
        dishFlavorMapper.DeleterDishFlavor(dish.getId());

        //向口味表添加多条数据
        List<DishFlavor> flavors = dto.getFlavors();
          flavors.forEach(dishFlavor ->{
              dishFlavor.setDishId(dish.getId());

          });

          dishFlavorMapper.UpdateDishFlavor(flavors);
    }

    @Override
    @Transactional   //启用事务
    public Result<String> deleterdish(List<Long> ids) {
        //1.查询删除的菜品是否有在起售中的
        Integer count = dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE);

        if (count >0){

            throw  new DeletionNotAllowedException("正在启售的菜品不能删除!!!");
        }
        //2.判断当前菜品有没有被套餐引用
       Integer count2 =  setmealDishMapper.selectSetmealCountByDishId(ids);

        if (count2 >0){
            throw  new DeletionNotAllowedException("被套餐引用的菜品不能删除！！！");


        }
        //3.删除dish表中的数据
        dishMapper.deleterdish(ids);

        //4.删除dish_flavor表中的数据

        dishFlavorMapper.deleterflavor(ids);



        return Result.success();
    }

    @Override
    public void DishActivation(Long id, Integer status) {

        dishMapper.DishActivation(id,status);

    }

    @Override
    public List<Dish> list(Long categoryId) {

       Dish dish =  Dish.builder().categoryId(categoryId).status(1).build();



        return dishMapper.list(dish);
    }


}
